/**
 * 入库记录storeInRecordDesc.js
 * 版本号： v1.0.0
 */
$(function () {
    //统一入库标识
    var sameRecord = $("#sameRecord").val();
    var lastChecked = null;
    $("#storeInRecordDesc_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/storeInRecord/storeInRecordDescList.do?sameRecord=' + sameRecord,
        idField: 'stirId',
        frozenColumns: [[
            {field: 'stirId', checkbox: true},
        ]],
        columns: [
            [
                {field: 'materialName', title: '材料名称', width: 120, fixed: true},
                {field: 'standardType', title: '材料规格', width: 160,fixed: true},
                {field: 'storeNum', title: '入库数量', width: 120, fixed: true},
                {field: 'meteringUnit', title: '计量单位', width: 80, fixed: true},
                {field: 'supName', title: '材料供应商', width: 130, fixed: true},
                {field: 'pushPerson', title: '入库人', width: 80,fixed: true},
                {field: 'pushDate', title: '入库时间', width: 120}
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#storeInRecordDesc_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#storeInRecordDesc_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#storeInRecordDesc_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#storeInRecordDesc_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });
});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            storeInRecordDesc_findList();
                        }
                    });
                }
                //查看详细按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            storeInRecordDesc_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

//入库记录查询
function storeInRecordDesc_findList() {
    refreshPageNumber("storeInRecordDesc_dataList");
    $("#storeInRecordDesc_dataList").datagrid("reload",{
        materialName:$("#materialName").val(),
        standardType:$("#standardType").val()
    });
    //清楚选中项
    $("#storeInRecordDesc_dataList").datagrid("clearSelections");
}

/**
 * 查看入库记录详情
 */
function storeInRecordDesc_view(iconCls){
    var row = $("#storeInRecordDesc_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的入库记录详情", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(rkjl.id,rkjl.url + row.stirId, iconCls, rkjl.closable,rkjl.title);
}